unit DLLLoader;

interface

uses
  windows, Classes, SysUtils, Forms;

type
  TDLLLoader = class(TObject)
  private
    FHandle: THandle;
    FFileName: string;
    procedure SetFileName(const Value: string);
  protected
    function GetApplicationPath: string;
  public
    constructor Create(DllFileName: string);
    destructor destroy(); override;
    procedure FreeHandle(); virtual;
    property FileName: string read FFileName write SetFileName;
    property DLLHandle: THandle read FHandle;
  end;

implementation

  { TDLLLoader }//

constructor TDLLLoader.Create(DllFileName: string);
begin
  FileName := DLLFileName;
end;

destructor TDLLLoader.destroy;
begin
  FreeHandle;
  inherited;
end;

procedure TDLLLoader.FreeHandle;
begin
  if FHandle <> 0 then
    FreeLibrary(FHandle);
end;

function TDLLLoader.GetApplicationPath: string;
begin
  result := ExtractFilePath(Application.ExeName);
end;

procedure TDLLLoader.SetFileName(const Value: string);
begin
  if FFileName <> Value then
  begin
    FFileName := Value;
    FreeHandle;
    FHandle := LoadLibrary(PChar(FFileName));
    if FHandle = 0 then
    begin
      raise Exception.Create('Load Library falut, maybe the ' + FFileName + ' not exists!');
    end;
  end;
end;

end.

